package com.fuhe.chen.vendingmachine.service.impl;

import com.fuhe.chen.vendingmachine.common.MachineCache;
import com.fuhe.chen.vendingmachine.common.redis.RedisUtils;
import com.fuhe.chen.vendingmachine.common.Utils;
import com.fuhe.chen.vendingmachine.dao.ContainerDao;
import com.fuhe.chen.vendingmachine.dao.MachineDao;
import com.fuhe.chen.vendingmachine.pojo.*;
import com.fuhe.chen.vendingmachine.service.IBookOrderService;
import com.fuhe.chen.vendingmachine.service.ICommodityService;
import com.fuhe.chen.vendingmachine.service.IMachineService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static com.fuhe.chen.vendingmachine.constant.CacheConstant.MACHINENAME_ALL;

@Service
public class MachineServiceImpl implements IMachineService {

    public static final Logger LOGGER = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Autowired
    ICommodityService commodityService;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    MachineDao machineDao;

    @Autowired
    ContainerDao containerDao;

    @Autowired
    IBookOrderService bookOrderService;

    @Override
    @Transactional
    public void addMachine(Machine machine) {
        machineDao.addMachine(machine);
        if (machine.getType() == 0) {
            List<Container> containers = new ArrayList<>();
            for (int i = 1; i <= machine.getCapacity(); i++) {
                Container container = new Container();
                container.setContainer_number(Utils.getContainerFormatString(String.valueOf(i)));
                container.setMachine_id(machine.getId());
                containers.add(container);
            }
            containerDao.addContainerWithMachine(containers);
        }
        redisUtils.delAll(MACHINENAME_ALL);
    }

    @Override
    public void updateMachine(Machine machine) {
        machineDao.updateMachine(machine);
        redisUtils.delAll(MACHINENAME_ALL);
    }

    @Override
    public PageInfo<Machine> findAllForAdmin(int page, int size) {
        String key = MACHINENAME_ALL + page + ":" + size;
        if (redisUtils.hasKey(key)) {
            return (PageInfo<Machine>) redisUtils.get(key);
        } else {
            PageHelper.startPage(page, size);
            List<Machine> machines = machineDao.findAll();
            PageInfo<Machine> pageInfo = new PageInfo<>(machines);
            redisUtils.set(key, pageInfo);
            return pageInfo;
        }
    }

    public List<Machine> findAll() {
        List<Machine> machines = machineDao.findAll();
        for (Machine machine : machines) {
            if (machine.getType() == 0) {
                Integer capacity = MachineCache.getCapacity(machine.getId());
                machine.setCapacity(capacity);
            }
        }
        return machines;
    }

    @Override
    public Machine findOne(Integer machineId) {
        return machineDao.findOne(machineId);
    }

    @Override
    public List<Container> queryContainer(Integer machineId) {
        List<Container> byMachineId = containerDao.findByMachineId(machineId);

        return byMachineId;
    }

    @Override
    public List<Container> queryEmptyContainer(Integer machineId) {
        List<Container> containers = containerDao.findEmptyContainerByMachineId(machineId);
        return containers;
    }
}
